/**
 * @分函数编写单链表
 * 
 * 
 * @1.链表创建 √
 * @2.链表长度 √
 * @3.链表打印 √
 * @4.查找节点 √ 返回位置
 * @5.删除节点 √
 * @6.增加节点 √
 * @7.修改节点 √
 * 
 * 
 * @Progra：Zbooo
 **/

#include <iostream>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
};

ListNode *CreatNode(int &n) //创建n个节点的链表
{
    ListNode *head, *node, *end;
    head = new ListNode;
    end = head;
    for (int i = 0; i < n; i++)
    {
        node = new ListNode;
        cin >> node->val;
        end->next = node;
        end = node;
    }
    end->next = nullptr;
    return head;
}

void PrintList(ListNode *head) //打印链表
{
    ListNode *p = head;
    while (p->next != nullptr)
    {
        p = p->next;
        cout << p->val << " ";
    }
}

int length(ListNode *head) //返回链表的长度
{
    ListNode *p = new ListNode;
    p = head;
    int len = 0;
    while (p->next != nullptr)
    {
        p = p->next;
        len++;
    }
    return len;
}

int FindNode(ListNode *head, const int &n) //查找链表中是否存在n，存在返回位置，不存在返回-1
{
    ListNode *p = new ListNode;
    p = head;
    int Pos = 0;
    while (p->next != nullptr)
    {
        p = p->next;
        Pos++;
        if (p->val == n)
            return Pos;
    }
    return -1;
}

bool DeleteNode(ListNode *head, const int &n) //删除链表的第n个节点
{
    if (length(head) < n)
        return false;
    ListNode *temp = new ListNode;
    for (int i = 0; i < n - 1; i++)
    {
        head = head->next;
    }
    temp = head->next;
    head->next = head->next->next;
    delete (temp);
    return true;
}

bool AddNode(ListNode *head, const int Pos, const int &value) //添加一个节点，在pos这个位置添加一个数值为value的节点
{
    if (length(head) < Pos)
        return false;
    for (int i = 0; i < Pos - 1; i++)
    {
        head = head->next;
    }
    ListNode *temp = new ListNode;
    temp->val = value;
    temp->next = head->next;
    head->next = temp;
    return true;
}

bool ChangeNode(ListNode *head, const int Pos, const int &Value) //修改节点 修改位置pos上的值为value
{
    if (length(head) < Pos)
        return false;
    for (int i = 0; i < Pos; i++)
    {
        head = head->next;
    }
    head->val = Value;
    return true;
}

int main()
{
    int n;
    cin >> n;
    ListNode *head = new ListNode;
    head = CreatNode(n);
    /* DeleteNode(head, 3);
    AddNode(head, 3, 10);
    ChangeNode(head, 3, 10); */
    cout << FindNode(head, 3);
    PrintList(head);
    system("pause");
    return 0;
}